package com.cxg.fairy.ec.main.index.ad;

import android.graphics.Paint;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cxg.fairy.ui.recycler.MultipleFields;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.ui.recycler.MultipleRecyclerAdapter;
import com.cxg.fairy.ui.recycler.MultipleViewHolder;
import com.diabin.latte.ec.R;

import java.util.List;

/**
 * Created by CFairy
 */

public final class ADGoodsListAdapter extends MultipleRecyclerAdapter {

    private static final RequestOptions OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .dontAnimate();

    ADGoodsListAdapter(List<MultipleItemEntity> data) {
        super(data);

        //添加购物测item布局
        addItemType(ADGoodsListItemType.AD_GOODS_LIST_HORIZONTAL_A,R.layout.item_ad_goods_list_horizontal_a);
        addItemType(ADGoodsListItemType.AD_GOODS_LIST_VERTICAL_A, R.layout.item_ad_goods_list_vertical_a);
        addItemType(ADGoodsListItemType.AD_GOODS_LIST_HEADER_A,R.layout.item_ad_goods_header_a);
    }

    @Override
    protected void convert(MultipleViewHolder holder, final MultipleItemEntity entity) {
        super.convert(holder, entity);

        //先取出所有值
        final String imgUrl = entity.getField(MultipleFields.IMAGE_URL);
        final String mainTitle = entity.getField(ADGoodsListItemFields.MAIN_TITLE);
        final String subTitle = entity.getField(ADGoodsListItemFields.SUB_TITLE);
        final String adGoodsName = entity.getField(ADGoodsListItemFields.AD_GOODS_NAME);
        final String adGoodsDescA = entity.getField(ADGoodsListItemFields.AD_GOODS_DESCRIPTION_A);
        final String adGoodsDescB = entity.getField(ADGoodsListItemFields.AD_GOODS_DESCRIPTION_B);
        final String defaultPrice = entity.getField(ADGoodsListItemFields.DEFAULT_PRICE);
        final String unit = entity.getField(ADGoodsListItemFields.UNIT);
        final String thumb = entity.getField(ADGoodsListItemFields.THUMB);
        final String price = entity.getField(ADGoodsListItemFields.PRICE);

        //所有控件
        final AppCompatImageView ivADGoodsHeaderImg = holder.getView(R.id.iv_img_ad_a);
        final AppCompatTextView tvADGoodsMainTitle = holder.getView(R.id.tv_main_title_ad_a);
        final AppCompatTextView tvADGoodsSubTitle = holder.getView(R.id.tv_sub_title_ad_a);
        final AppCompatImageView tvADGoodsThumb = holder.getView(R.id.tv_ad_goods_img_a);
        final AppCompatTextView tvADGoodsName = holder.getView(R.id.tv_ad_goods_name_a);
        final AppCompatTextView tvADGoodsDescA = holder.getView(R.id.tv_ad_goods_desc_a);
        final AppCompatTextView tvADGoodsDescB = holder.getView(R.id.tv_ad_goods_desc_b);
        final AppCompatTextView tvADGoodsDefaultPrice = holder.getView(R.id.tv_ad_goods_default_price_a);
//        final AppCompatTextView tvADGoodsDefaultPriceH = holder.getView(R.id.tv_ad_goods_default_price_h_a);
        final AppCompatTextView tvADGoodsUnit = holder.getView(R.id.tv_ad_goods_unit_a);
        final AppCompatTextView tvADGoodsPrice = holder.getView(R.id.tv_ad_goods_price_a);


        switch (holder.getItemViewType()) {

            case ADGoodsListItemType.AD_GOODS_LIST_HEADER_A:
                Glide.with(mContext)
                        .load(imgUrl)
                        .apply(OPTIONS)
                        .into(ivADGoodsHeaderImg);
                tvADGoodsMainTitle.setText(mainTitle);
                tvADGoodsSubTitle.setText(subTitle);
                break;
            case ADGoodsListItemType.AD_GOODS_LIST_VERTICAL_A:
                Glide.with(mContext)
                        .load(thumb)
                        .apply(OPTIONS)
                        .into(tvADGoodsThumb);
                tvADGoodsName.setText(adGoodsName);
                tvADGoodsDescA.setText(adGoodsDescA);
                tvADGoodsDescB.setText(adGoodsDescB);
                tvADGoodsDefaultPrice.setText(defaultPrice);
                tvADGoodsDefaultPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                tvADGoodsUnit.setText(unit);
                tvADGoodsPrice.setText(price);
                break;
            case ADGoodsListItemType.AD_GOODS_LIST_HORIZONTAL_A:
                Glide.with(mContext)
                        .load(thumb)
                        .apply(OPTIONS)
                        .into(tvADGoodsThumb);
                tvADGoodsName.setText(adGoodsName);
                tvADGoodsUnit.setText(unit);
                tvADGoodsPrice.setText(price);
                break;
            default:
                break;
        }

    }

    @Override
    public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
        return getData().get(position).getField(ADGoodsListItemFields.SPAN_SIZE);
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }
}
